﻿<%@ WebHandler Language="C#" Class="SysMessageHandlerService" %>

using System;
using System.Web;

using System.Collections;
using System.Collections.Generic;
using System.Reflection;

using XJX.BusinessLayer;

using XJX.Page;

public class SysMessageHandlerService : IHttpHandler, System.Web.SessionState.IRequiresSessionState
{

    public void ProcessRequest(HttpContext context)
    {
        context.Response.ContentType = "application/json";

        string response = "";

        String methodName = context.Request["method"];
        Type type = this.GetType();
        MethodInfo method = type.GetMethod(methodName);


        if (method == null)
        {
            response = new XJX.Page.JsonResult(XJX.Page.StatusMessageType.Hint, "调用的方法不存在！").ToJson(); return;
        }

        try
        {
            BeforeInvoke(methodName);
            response = method.Invoke(this, null).ToString();
        }
        catch (Exception ex)
        {
            Hashtable result = new Hashtable();
            result["error"] = -1;
            result["message"] = ex.Message;
            result["stackTrace"] = ex.StackTrace;
            String json = XJX.BusinessLayer.JSON.Encode(result);
            context.Response.Clear();
            context.Response.Write(json);
        }
        finally
        {
            AfterInvoke(methodName);
            HttpContext.Current.Response.Write(response);
        }
    }

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }

    //权限管理
    protected void BeforeInvoke(String methodName)
    {
        //Hashtable user = GetUser();
        //if (user.role == "admin" && methodName == "remove") throw .      
    }
    //日志管理
    protected void AfterInvoke(String methodName)
    {

    }


    //===============================================================================================================================================
    //===============================================================================================================================================
    //===============================================================================================================================================
    //===============================================================================================================================================
    //===============================================================================================================================================


    //获取某家庭所有信件信息表
    public string Get_One_Family_Message_From_UID()
    {
        FamilyInfo fi = (FamilyInfo)HttpContext.Current.Session["FamilyInfo"];
        string UID = fi.UID;

        int PageIndex = Convert.ToInt32(HttpContext.Current.Request["PageIndex"]);
        int PageSize = Convert.ToInt32(HttpContext.Current.Request["PageSize"]);

        return XJX.Page.SysMessage.Get_One_Family_Message_From_UID(UID, PageIndex, PageSize);
    }

    //获取某家庭所有未读信件数量，未进行JSON格式处理，直接输出为字符串数字。
    public string Get_One_Family_MessageNum_From_UID()
    {
        FamilyInfo fi = (FamilyInfo)HttpContext.Current.Session["FamilyInfo"];
        string UID = fi.UID;

        return XJX.Page.SysMessage.Get_One_Family_Message_From_UID(UID);
    }

    public string Del_One_Family_Message_From_ID()
    {
        string result = "";

        string ID = HttpContext.Current.Request["MessageID"];

        bool b = XJX.Page.SysMessage.Del_One_Family_Message_From_ID(ID);

        if (b)
        {
            result = new XJX.Page.JsonResult(XJX.Page.StatusMessageType.Success, "删除成功!").ToJson();
        }
        else
        {
            result = new XJX.Page.JsonResult(XJX.Page.StatusMessageType.Error, "删除失败!").ToJson();
        }
        return result;
    }
    
    //打开一条站内信息，则将它标记为已读
    public string Mark_One_SysMsg_Readed()
    {
        string result = "";

        string ID = HttpContext.Current.Request["MessageID"];

        bool b = XJX.Page.SysMessage.Mark_One_SysMsg_Readed(ID);

        if (b)
        {
            result = new XJX.Page.JsonResult(XJX.Page.StatusMessageType.Success, "标记成功!").ToJson();
        }
        else
        {
            result = new XJX.Page.JsonResult(XJX.Page.StatusMessageType.Error, "标记失败!").ToJson();
        }
        return result;
    }
}